package com.android.smsmessing.services;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;
import android.telephony.gsm.SmsManager;
import android.widget.Toast;

import com.android.smsmessing.model.SMSInboxVO;
import com.android.smsmessing.model.SMSVO;
import com.android.smsmessing.utils.StringUtils;

public class SMSService {
	static Uri uriSms = Uri.parse("content://sms");
	
	private static SMSService instance = null;
	public static SMSService getInstance(){
		if(instance == null){
			instance = new SMSService();
		}
		return instance;
	}
	/**
	 * 通过电话号码取得短信列表
	 * @param context		上下文
	 * @param number		电话号码
	 * @return	短信列表
	 */
	public  ArrayList<SMSVO> getSMSesByNumber(Context context,String number){
		ArrayList<SMSVO> smses = new ArrayList<SMSVO>();
		String personName = PeopleService.getInstance().getPeopleNameByNumber(context, number);
		Cursor c = context.getContentResolver()
		.query(uriSms, new String[] { "address", "date", "body","type"},
				"address=? or address=? or address=?", new String[]{number,"+86"+number,"12520"+number}, "date asc");
		while(c.moveToNext()){
			String address = c.getString(c
					.getColumnIndexOrThrow("address"));
			Long time = c.getLong(c.getColumnIndexOrThrow("date"));
			String body = c.getString(c.getColumnIndexOrThrow("body"));
			int type = c.getInt(c.getColumnIndexOrThrow("type"));
			
			SMSVO sms = new SMSVO();
			sms.isSelect = false;
			sms.name = personName;
			sms.mobile = number;
			sms.time = time;
			sms.detail = body;
			sms.type = type;
			smses.add(sms);
		}
		return smses;
	}
	
	/**
	 * 获取短信收发件箱列表
	 * @param context	上下文
	 * @return	收件箱列表
	 */
	public  HashMap<Integer,SMSInboxVO> getSMSInboxsToMap(Context context){
		int index = 0;
		HashMap<String, SMSInboxVO> map = new HashMap<String, SMSInboxVO>();
		HashMap<Integer,SMSInboxVO> keyMap = new HashMap<Integer, SMSInboxVO>();
		Cursor c = context.getContentResolver()
		.query(uriSms, new String[] { "address", "date", "person","body" },
				null, null, null);
		while (c.moveToNext()) {
			String address = c.getString(c.getColumnIndexOrThrow("address"));
			String personId = c.getString(c.getColumnIndexOrThrow("person"));
			Long date = c.getLong(c.getColumnIndexOrThrow("date"));
			String body = c.getString(c.getColumnIndexOrThrow("body"));
			//如果是+86开头,那么过滤+86
			if(address != null){
				address = StringUtils.cutMobileNumber(address);
			}
			// 判断map里面是否存在该电话号码
			SMSInboxVO smsInbox = map.get(address);
			if (smsInbox != null) {
				smsInbox.total += 1;
			} else {
				smsInbox = new SMSInboxVO();
				smsInbox.time = date;
				smsInbox.total = 1;
				smsInbox.summary = body;
				smsInbox.mobile = address;
				smsInbox.index = index;
				
				// 如果联系人ID不为空的话
					if (address != null && StringUtils.isEmpty(smsInbox.name)) {
						//根据电话号码获取联系人姓名
						String personName = PeopleService.getInstance().getPeopleNameByNumber(context, address);
						if (personName != null) {
							smsInbox.name = personName;
						}else{
							smsInbox.name = address;
						}
					} 
					index ++;
			}
			map.put(address, smsInbox);
			keyMap.put(smsInbox.index, smsInbox);
		}
		return keyMap;
	}
	
	/**
	 * 发送短信
	 * @param phoneNumber		手机号码
	 * @param message			消息
	 * @param sentPI			
	 * @param deliveredPI
	 * @date 2010-3-17
	 */
	public void sendSMS(Context context,String phoneNumber, String message,PendingIntent sentPI,PendingIntent deliveredPI){
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
		//保存发送消息
		insertSendSMS(context,phoneNumber,message);
	}
	
	/**
	 * 插入发送消息
	 * @param context		上下文
	 * @param phoneNumber	手机号码
	 * @param message		消息
	 * @date 2010-3-17
	 */
	public void insertSendSMS(Context context,String phoneNumber,String message){
        ContentValues values = new ContentValues();
        values.put("address", phoneNumber);
        values.put("body", message);
        context.getContentResolver().insert(Uri.parse("content://sms/sent"), values);
	}

	
	
	

}
